﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using BHXH.Repository;
using BHXH.LinqRepository;

namespace BHXH2014.Phieu.PhieuA
{
    public partial class FrmDanhSachPhieuA : FrmDanhSachPhieu
    {
        DanhMucPhieuRepository _repPhieu = new DanhMucPhieuRepository();
        ChiTietPhieuRepository _repChiTietPhieu = new ChiTietPhieuRepository();
        DanhMucPhieu_DangBangRepository _repPhieuDangBang = new DanhMucPhieu_DangBangRepository();
        public FrmDanhSachPhieuA()
        {
            InitializeComponent();
        }
        protected override void LoadData()
        {
            try
            {
                //GetAllByLoaiPhieu
                gridControlDs.DataSource = _repPhieu.GetAllByLoaiPhieu(ELoaiPhieu.PhieuA.ToString());
                //gridControlDs.DataSource = Lists.DanhMucPhieu.Filter("LoaiPhieu = 'PhieuA'").DataView;
                //Lists.DanhMucPhieu_DangBang.Instance.LoadFromDB();
                //Lists.DanhMucChiTietPhieu.Instance.LoadFromDB();
                //Lists.DanhMucKienNghi.Instance.LoadFromDB();
            }
            catch (Exception ex)
            {
                FrmExceptionNotifier.ShowAndLog(ex);
            }
        }
        public static void CreateNew(bool isReSet)
        {
            FrmEditPhieuA frm = new FrmEditPhieuA(null);
            frm.ShowDialog();
            //Lists.DanhMucChiTietPhieu.Instance.LoadFromDB();
            //Lists.DanhMucPhieu_DangBang.Instance.LoadFromDB();
            //Lists.DanhMucPhieu.Instance.LoadFromDB();
            //LoadData();
        }

        protected override void CreateNew()
        {
            FrmEditPhieuA frm = new FrmEditPhieuA(null);
            frm.ShowDialog();
            //Lists.DanhMucChiTietPhieu.Instance.LoadFromDB();
            //Lists.DanhMucPhieu_DangBang.Instance.LoadFromDB();
            //Lists.DanhMucPhieu.Instance.LoadFromDB();
            LoadData();
        }
        protected override void Edit()
        {
            //DataRow row = gridViewDs.GetFocusedDataRow();
            Guid id = (Guid)gridViewDs.GetRowCellValue(gridViewDs.FocusedRowHandle, "ID");
            //Lists.Phieu phieuA = Lists.DanhMucPhieu.Instance.GetByID((Guid)row["ID"]);
            var phieuA = _repPhieu.GetOneById(id);
            if (phieuA == null) return;
            FrmEditPhieuA frm = new FrmEditPhieuA(phieuA);
            frm.ShowDialog();
            LoadData();

        }
        protected override void Delete()
        {
            if (MessageBox.Show("Bạn có thực sự muốn xóa?", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Hand) == DialogResult.Yes)
            {

                //DataRow row = gridViewDs.GetFocusedDataRow();
                //Lists.Phieu phieuA = Lists.DanhMucPhieu.Instance.GetByID((Guid)row["ID"]);

                //phieuA.DeleteFromDBAndAllRelation();
                Guid id = (Guid)gridViewDs.GetRowCellValue(gridViewDs.FocusedRowHandle, "ID");
                var phieuA = _repPhieu.GetOneById(id);
                if (phieuA == null) return;
                DeletePhieu(phieuA);
                LoadData();
            }
        }

        void DeletePhieu(BHXH.LinqRepository.Phieu phieu)
        {
            _repPhieuDangBang.DeleteAllByPhieuId(phieu.ID);
            _repChiTietPhieu.DeleteAllByPhieuId(phieu.ID);
            _repPhieu.DeleteOne(phieu);
            _repPhieuDangBang.SubmitChanges();
            _repChiTietPhieu.SubmitChanges();
            _repPhieu.SubmitChanges();
        }
    }
}
